const { GraphQLServer } = require('graphql-yoga')
const { Prisma } = require('prisma-binding')
// const redis = require('redis')

const resolvers = require('./resolvers')

console.log(process.env.NODE_ENV)

const isProd = process.env.NODE_ENV === 'production'

/* const client = redis.createClient()

client.on('error', function (err) {
  console.log('Error ' + err)
}) */

const server = new GraphQLServer({
  typeDefs: 'src/schema.graphql',
  resolvers,
  context: req => ({
    // redis: client,
    ...req,
    db: new Prisma({
      // the Prisma DB schema
      typeDefs: 'src/generated/prisma.graphql',
      // the endpoint of the Prisma DB service (value is set in .env)
      endpoint: process.env.PRISMA_ENDPOINT,
      // taken from database/prisma.yml (value is set in .env)
      secret: process.env.PRISMA_SECRET,
      // log all GraphQL queries & mutations
      debug: !isProd
    })
  })
})

server.start(() => console.log('Server is running on http://localhost:4000'))
